package org.skyfree.ghyll.tcard.wizard;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.skyfree.ghyll.tcard.Activator;
import org.skyfree.ghyll.tcard.core.ITProject;
import org.skyfree.ghyll.tcard.core.TCardCore;

public class ChooseProjectDialog extends Dialog implements ISelectionChangedListener 
{

	ITProject selectProject = null;
	
	Image PROJECT = Activator.getImageDescriptor("icons/project.png").createImage();
	
	class TreeLabelProvider extends LabelProvider {
		public String getText(Object element) 
		{
			ITProject project = (ITProject)element;
			return project.getName();
		}
		public Image getImage(Object element) {
			return PROJECT;
		}
	}
	class TreeContentProvider implements IStructuredContentProvider, ITreeContentProvider {
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object inputElement) 
		{
			return TCardCore.getDefault().getWorkSpace().getProjects();
		}
		public Object[] getChildren(Object parentElement) {
			return new Object[0];
		}
		public Object getParent(Object element) {
			return null;
		}
		public boolean hasChildren(Object element) {
			return getChildren(element).length > 0;
		}
	}
	
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public ChooseProjectDialog(Shell parentShell) {
		super(parentShell);
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new FillLayout());

		final TreeViewer treeViewer = new TreeViewer(container, SWT.BORDER);
		treeViewer.setLabelProvider(new TreeLabelProvider());
		treeViewer.setContentProvider(new TreeContentProvider());
		treeViewer.setInput(new Object());
		treeViewer.addSelectionChangedListener(this);
		treeViewer.getTree().setLinesVisible(false);
		//
		return container;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 375);
	}

	@Override
	public void selectionChanged(SelectionChangedEvent event) 
	{
		IStructuredSelection select = (IStructuredSelection) event.getSelection();
		this.selectProject = (ITProject) select.getFirstElement();
	}

	public ITProject getSelectProject() {
		return selectProject;
	}

	
}
